package com.mspbots.core.teams.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mspbots.core.AbstractBaseServiceImpl;
import com.mspbots.core.teams.mapper.TeamsUserNoMessageMapper;
import com.mspbots.core.teams.model.TeamsUserNoMessage;
import com.mspbots.core.teams.service.TeamsUserNoMessageService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Log4j2
@Service
public class TeamsUserNoMessageServiceImpl extends AbstractBaseServiceImpl<TeamsUserNoMessageMapper, TeamsUserNoMessage> implements TeamsUserNoMessageService {


    @Override
    public Boolean insertBatch(Long tenantId, List<TeamsUserNoMessage> list) {
        List<TeamsUserNoMessage> list1 = super.list(Wrappers.<TeamsUserNoMessage>lambdaQuery()
                .eq(TeamsUserNoMessage::getTenantId, tenantId));
        if (!CollectionUtils.isEmpty(list1)) {
            Map<String, Long> map = list1.stream().collect(Collectors.toMap(TeamsUserNoMessage::getTeamsUserId, TeamsUserNoMessage::getId, (key1, key2) -> key2));
            list.stream().filter(t -> map.containsKey(t.getTeamsUserId())).forEach(t -> t.setId(map.get(t.getTeamsUserId())));
        }
        return super.saveOrUpdateBatch(list);
    }
}
